<?php
header('Content-Type:text/html;charset=UTF-8');

include 'lib/checker.php';

@$pid = $_REQUEST['pid'];
@$img = $_FILES['img'];

@$oldname = $_REQUEST['oldname'];
@$newname = $_REQUEST['newname'];

@$oldmobiletext = $_REQUEST['oldmobiletext'];
@$newmobiletext = $_REQUEST['newmobiletext'];

@$oldQQtext = $_REQUEST['oldQQtext'];
@$newQQtext = $_REQUEST['newQQtext'];

@$oldhometext = $_REQUEST['oldhometext'];
@$newhometext = $_REQUEST['newhometext'];

@$oldofficetext = $_REQUEST['oldofficetext'];
@$newofficetext = $_REQUEST['newofficetext'];

$title = '修改联系人错误提示';
$jumpUrl = 'listperson.php';

if(empty($pid)){
	$message = '获取联系人信息出错';
	include 'lib/message.php';
	die;
}

$jumpUrl = 'editperson.php?pid='.$pid;

require 'lib/db.php';

if(!empty($img['name'])&&$img['error']==0){
	$flag = move_uploaded_file($img['tmp_name'], 'imgs/'.$img['name']);
	if(!$flag){
		$message = '联系人的照片上传出错';
		include 'lib/message.php';
		die;
	}
	$flag = dbUpdate('update person set imgUrl=:imgUrl where id=:id',array('imgUrl'=>$img['name'],'id'=>$pid));
	if(!flag){
		$message = '联系人的照片上传失败';
		include 'lib/message.php';
		die;
	}
}

if(!empty($newname)&&$newname!=$oldname){
	$flag = dbUpdate('update person set name=:name where id=:id',array('name'=>$newname,'id'=>$pid));
	if(!flag){
		$message = '修改联系人姓名失败';
		include 'lib/message.php';
		die;
	}
}

if(!empty($newmobiletext)&&$newmobiletext!=$oldmobiletext){
	$flag = dbUpdate("update number set content=:content where type='手机' and pid=:pid",array('content'=>$newmobiletext,'pid'=>$pid));
	if(!flag){
		$message = '修改联系人手机号码失败';
		include 'lib/message.php';
		die;
	}
}

if(!empty($newQQtext)&&$newQQtext!=$oldQQtext){
	$flag = dbUpdate("update number set content=:content where type='QQ' and pid=:pid",array('content'=>$newQQtext,'pid'=>$pid));
	if(!flag){
		$message = '修改联系人QQ号码失败';
		include 'lib/message.php';
		die;
	}
}

if(!empty($oldhometext)&&$oldhometext!=$oldhometext){
	$flag = dbUpdate("update number set content=:content where type='家庭' and pid=:pid",array('content'=>$oldhometext,'pid'=>$pid));
	if(!flag){
		$message = '修改联系人家庭号码失败';
		include 'lib/message.php';
		die;
	}
}

if(!empty($newofficetext)&&$newofficetext!=$oldofficetext){
	$flag = dbUpdate("update number set content=:content where type='办公' and pid=:pid",array('content'=>$newofficetext,'pid'=>$pid));
	if(!flag){
		$message = '修改联系人办公号码失败';
		include 'lib/message.php';
		die;
	}
}

header('location:editPerson.php?pid='.$pid);



